//
//  LocalDataManager.m
//  SZBank
//
//  Created by LuoYao on 16/7/8.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LocalDataManager.h"


@implementation LocalDataManager

//本地图片管理
+ (UIImage *)getImageWithSourceOfPath:(NSString *)imageName{
    if (imageName.length == 0 || imageName == nil) {
        return [[UIImage alloc]init];
    }
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
    return image;
}


+ (UIImage *)getImageWithSourceOfPathForJPG:(NSString *)imageName{
    if (imageName.length == 0 || imageName == nil) {
        return [[UIImage alloc]init];
    }
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]];
    return image;
}

+ (UIImage *) getImageWithURL:(NSString *)URLStr
{
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLStr]];
    UIImage  *image = [UIImage imageWithData:data];
    return image;
}
+ (void)addAlertControllerWithControllerTitle:(NSString *)controllerTitle controllerMessage:(NSString *)controllerMessage actionSureTitle:(NSString *)actionSureTitle actionCancelTile:(NSString *)actionCancelTile actionSureBlock:(alertBlock )actionSureBlock  actionCancelBlock:(alertBlock )actionCancelBlock style:(alertStyle)style{
    //弹框控制器
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:controllerTitle message:controllerMessage preferredStyle:UIAlertControllerStyleAlert];
 
 
    if (style == 0) {
        //弹框操作
        UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:actionSureTitle style:UIAlertActionStyleDefault handler:actionSureBlock];
         [alertController addAction:sureAlertAction];
    }else{
        //弹框操作
        UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:actionSureTitle style:UIAlertActionStyleDefault handler:actionSureBlock];
        //弹框操作
        UIAlertAction *cancelAlertAction = [UIAlertAction actionWithTitle:actionCancelTile style:UIAlertActionStyleCancel handler:actionCancelBlock];
    
        [alertController addAction:sureAlertAction];
        [alertController addAction:cancelAlertAction];
    }
    //推送
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}


//获取备忘录
+ (NSArray *)getMemorandumWithPlist{
    
    //获取沙盒路径
    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    //新建plist文件路径
    NSString *plistPath = [sandBoxPath stringByAppendingString:@"/Memorandum.plist"];
    
    NSArray *array =  nil;
    
    //判断路径下是否有文件，如果没有则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        array = [NSArray array];
    }else{
        array = [NSArray arrayWithContentsOfFile:plistPath];
    }
    
    return array;
}

//存储备忘录
+ (void)saveMemorandumWithDictionary:(NSDictionary *)dic{
    //获取沙盒路径
    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    //新建plist文件路径
    NSString *plistPath = [sandBoxPath stringByAppendingString:@"/Memorandum.plist"];
    
    NSMutableArray *array = nil;
    
    //判断路径下是否有文件，如果没有则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        array = [NSMutableArray array];
    }else{
        array = [NSMutableArray arrayWithContentsOfFile:plistPath];
    }
    
    //将字典添加到数据
    [array addObject:dic];
    
    //存储
    if ([array writeToFile:plistPath atomically:YES]) {
        NSLog(@"%@",sandBoxPath);
    }

}


//根据下标删除对应的数据
+ (void)removeMemorandumWithIndex:(NSInteger)index{
    
    //获取沙盒路径
    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    //新建plist文件路径
    NSString *plistPath = [sandBoxPath stringByAppendingString:@"/Memorandum.plist"];
    
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
    
    //删除数组内数据
    [array removeObjectAtIndex:index];
    
    //存储到本地
    if ([array writeToFile:plistPath atomically:YES]) {
        NSLog(@"%@",sandBoxPath);
    }
    
}


//测试图片存储
+ (void)saveImageWithData:(NSData *)imageData{
    //获取沙盒路径
    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    //新建plist文件路径
    NSString *imagePath = [sandBoxPath stringByAppendingString:@"/headerImage.jpg"];
    
    //存储
    if ([imageData writeToFile:imagePath atomically:YES]) {
        NSLog(@"%@",sandBoxPath);
        //发通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"header" object:self];
    }
}


//测试获取图片
+ (NSData *)getImageDataWithPlist{
    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *imagePath = [sandBoxPath stringByAppendingString:@"/headerImage.jpg"];
    
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
    
    return imageData;
    
}
@end
